﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iRemoteMulti.Class
{
    class ErrorAndStatus
    {

        public static String error_info; //错误信息
        public static String status_info; //状态信息

        /*
         * 比较本次和上一次收到的数据，根据错误位的状态变化组成错误显示字符串
         */
        public static String getErrorInfo(Byte[] old_data,Byte[] new_data) {

            error_info = "";
            String error_prefix = System.DateTime.Now.ToString();
            int SPA_DPA = DataFormat.getNeedByteBit(new_data,24,6);

            if (isError(old_data, new_data, 19, 8))
            {
                error_info += error_prefix + " Error: Rectifier Module Over Temperature!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 7))
            {
                error_info += error_prefix + " Error: Transformer Over Temperature!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 6))
            {
                error_info += error_prefix + " Error: Resistor Over Temperature!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 5))
            {
                error_info += error_prefix + " Error: Rear Door Open!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 4))
            {
                if (SPA_DPA == 0)
                {
                    error_info += error_prefix + " Error: JR1_Blower Motor Overload!" + System.Environment.NewLine;
                }
                else
                {
                    error_info += error_prefix + " Error: JR1_Water Cooling System!" + System.Environment.NewLine;
                }
            }
            if (isError(old_data, new_data, 19, 3))
            {
                error_info += error_prefix + " Error: JR2_Field Overload!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 2))
            {
                error_info += error_prefix + " Error: Synchronous shaker Fault!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 19, 1))
            {
                error_info += error_prefix + " Amplifier is soft stopped!" + System.Environment.NewLine;
            }
            
            if (isError(old_data, new_data, 20, 8))
            {
                error_info += error_prefix + " Error: Shaker Over Travel!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 7))
            {
                error_info += error_prefix + " Error: Shaker Over Temperature!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 6))
            {
                if (SPA_DPA == 0)
                {
                    error_info += error_prefix + " Error: Shaker Cooling Air Pressure Low!" + System.Environment.NewLine;
                }
                else
                {
                    error_info += error_prefix + " Error: Shaker Water Leakage!" + System.Environment.NewLine;
                }
            }
            if (isError(old_data, new_data, 20, 5))
            {
                error_info += error_prefix + " Error: Shaker Body Isolator Over Travel!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 4))
            {
                error_info += error_prefix + " Error: Slip Table Oil Pressure Low!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 3))
            {
                error_info += error_prefix + " Error: Centering System PCC Fault!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 2))
            {
                error_info += error_prefix + " Error: Aux interlock 1!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 20, 1))
            {
                error_info += error_prefix + " Error: Aux interlock 2!" + System.Environment.NewLine;
            }

            if (isError(old_data, new_data, 21, 8))
            {
                error_info += error_prefix + " Error: Output Over Current!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 7))
            {
                error_info += error_prefix + " Error: Output Over Voltage!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 6))
            {
                error_info += error_prefix + " Error: Input Under Voltage!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 5))
            {
                error_info += error_prefix + " Error: Input Over Voltage!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 4))
            {
                error_info += error_prefix + " Error: Field Under Current!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 3))
            {
                error_info += error_prefix + " Error: Field Over Current!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 2))
            {
                error_info += error_prefix + " Error: Drive Power of Power Module Lost!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 21, 1))
            {
                error_info += error_prefix + " Error: Wrong PWM Drive Pulse!" + System.Environment.NewLine;
            }

            if (isError(old_data, new_data, 22, 8))
            {
                error_info += error_prefix + " Error: Slave Logic Module Fault!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 22, 7))
            {
                error_info += error_prefix + " Error: Power Module Over Temperature!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 22, 6))
            {
                error_info += error_prefix + " Error: Power Module Over Current!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 22, 5))
            {
                error_info += error_prefix + " Error: Power Module Under Voltage!" + System.Environment.NewLine;
            }
            if (isError(old_data, new_data, 22, 4))
            {
                error_info += error_prefix + " Error: PWM PCB Power Lost!" + System.Environment.NewLine;
            }

            return error_info;
        }

        /*
         * 比较本次和上一次收到的数据，根据状态位的状态变化组成状态变化字符串（显示在Status状态中）
         */
        public static String getStatusInfoOther(Byte[] old_data, Byte[] new_data)
        {

            status_info = "";
            String status_prefix = System.DateTime.Now.ToString();
            int SPA_DPA = DataFormat.getNeedByteBit(new_data, 24, 6);

            if (isStatusHighToLower(old_data, new_data, 22, 3))
            {
                status_info += status_prefix + " Warning: Shaker Emergency Stop!" + System.Environment.NewLine;
            }
            if (isStatusLowerToHigh(old_data, new_data, 23, 5))
            {
                status_info += status_prefix + " Warning: Local Emergency Stop!" + System.Environment.NewLine;
            }

            if (isStatusLowerToHigh(old_data, new_data, 24, 7))
            {
                status_info += status_prefix + " Warning: Remote Emergency Stop!" + System.Environment.NewLine;
            }

            if (isStatusLowerToHigh(old_data, new_data, 24, 8))
            {
                status_info += status_prefix + " Status: Remote mode is Enabled!" + System.Environment.NewLine;
            }
            if (isStatusHighToLower(old_data, new_data, 24, 8))
            {
                status_info += status_prefix + " Status: Remote mode is Disabled!" + System.Environment.NewLine;
            }

            return status_info;
        }
        /*
         * 比较本次和上一次收到的数据，根据状态位的状态变化组成状态变化字符串（显示在错误日志里）
         */
        public static String getStatusInfo(Byte[] old_data, Byte[] new_data)
        {

            status_info = "";
            String status_prefix = System.DateTime.Now.ToString();
            int SPA_DPA = DataFormat.getNeedByteBit(new_data, 24, 6);

            if (isStatusHighToLower(old_data, new_data, 22, 3))
            {
                status_info += status_prefix + " Warning: Shaker Emergency Stop!" + System.Environment.NewLine;
            }
            
            if (isStatusHighToLower(old_data, new_data, 22, 2))
            {
                status_info += status_prefix + " Status: The Field Power is switched on!" + System.Environment.NewLine;
            }
            if (isStatusLowerToHigh(old_data, new_data, 22, 2))
            {
                status_info += status_prefix + " Status: The Field Power is switched off!" + System.Environment.NewLine;
            }
            
            if (isStatusHighToLower(old_data, new_data, 22, 1))
            {
                status_info += status_prefix + " Status: The Main Power is switched on!" + System.Environment.NewLine;
            }
            if (isStatusLowerToHigh(old_data, new_data, 22, 1))
            {
                status_info += status_prefix + " Status: The Main Power is switched off!" + System.Environment.NewLine;
            }
            
            if (isStatusHighToLower(old_data, new_data, 23, 7))
            {
                status_info += status_prefix + " Status: The ‘GAIN POT’ is enabled!" + System.Environment.NewLine;
            }
            if (isStatusLowerToHigh(old_data, new_data, 23, 7))
            {
                status_info += status_prefix + " Status: The ‘GAIN POT’ is reset!" + System.Environment.NewLine;
            }
            
            if (isStatusHighToLower(old_data, new_data, 23, 6))
            {
                status_info += status_prefix + " Status: The output is enabled, now can write gain to amplifier!" + System.Environment.NewLine;
            }
            if (isStatusLowerToHigh(old_data, new_data, 23, 6))
            {
                status_info += status_prefix + " Status: the output is disabled!" + System.Environment.NewLine;
            }
            
            if (isStatusLowerToHigh(old_data, new_data, 23, 5))
            {
                status_info += status_prefix + " Warning: Local Emergency Stop!" + System.Environment.NewLine;
            }

            if (isStatusLowerToHigh(old_data, new_data, 24, 7))
            {
                status_info += status_prefix + " Warning: Remote Emergency Stop!" + System.Environment.NewLine;
            }

            if (isStatusLowerToHigh(old_data, new_data, 24, 8))
            {
                status_info += status_prefix + " Status: Remote mode is Enabled!" + System.Environment.NewLine;
            }
            if (isStatusHighToLower(old_data, new_data, 24, 8))
            {
                status_info += status_prefix + " Status: Remote mode is Disabled!" + System.Environment.NewLine;
            }

            return status_info;
        }

        /*
         * 判断是否是错误
         */
        public static bool isError(Byte[] old_data, Byte[] new_data,int index,int bit_index)
        {
            int old_bit = DataFormat.getNeedByteBit(old_data, index, bit_index);
            int new_bit = DataFormat.getNeedByteBit(new_data, index, bit_index);
            if (new_bit == 0 && old_bit == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /*
         * 判断从高位切到低位的状态变化
         */
        public static bool isStatusHighToLower(Byte[] old_data, Byte[] new_data, int index, int bit_index)
        {
            int old_bit = DataFormat.getNeedByteBit(old_data, index, bit_index);
            int new_bit = DataFormat.getNeedByteBit(new_data, index, bit_index);
            if (new_bit == 0 && old_bit == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /*
         * 判断从低位切到高位的状态变化
         */
        public static bool isStatusLowerToHigh(Byte[] old_data, Byte[] new_data, int index, int bit_index)
        {
            int old_bit = DataFormat.getNeedByteBit(old_data, index, bit_index);
            int new_bit = DataFormat.getNeedByteBit(new_data, index, bit_index);
            if (new_bit == 1 && old_bit == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
